﻿package gui.hug; 

  import flash.events.Event;
  
  /**
   * @author Alexey Kharkov
   * @modified by Natalia Ukhorskaya
   */
class HUGRadioButtonsGroup extends HUGSprite
  {
  	private var items: Array<HUGRadioButton>;
    private var sel:HUGRadioButton; // Currently selected RadioButton
	
	//public var selectedItem(getSelectedItem, setSelectedItem) : HUGRadioButton;
	//public var selectedIndex(getSelectedIndex, setSelectedIndex) : Int;

    public function new( x:Int, y:Int )
    {
	  super();
	  
	  sel = null;
	  
      this.x = x;
      this.y = y;

      items = new Array();
    }
    
    public function addRadioButton( label:String, x:Int, y:Int ):HUGRadioButton
    {
      var but:HUGRadioButton = new HUGRadioButton( label, items.length, this, x, y );
      addChild( but );
      items.push( but );
      return but;
    }
    
    public function getSelectedIndex():Int
    {
      return (sel == null) ? -1 : sel.index;
    }
    
    public function setSelectedIndex( idx:Int ): Int
    {
      var item:HUGRadioButton = null;
      if ( idx >= 0  &&  idx < items.length )
        item = items[idx];
      
      setSelectedItem(item);
	  return idx;
    }
    
    public function getSelectedItem():HUGRadioButton
    {
      return sel;
    }
    
    public function setSelectedItem( item:HUGRadioButton ): HUGRadioButton
    {
      if ( sel != null ) {
        sel.setChecked( false );
      }
	  sel = item;
      sel.setChecked( true );
      
      dispatchEvent( new Event( Event.CHANGE ) );
	  return sel;
    }
  }
